# data import 
rat_df = read.csv("data/rat_2020_2024.csv")
food_scrap_df = 
  read.csv("data/food_scrap_drop_off.csv")
zhvi_df = read.csv("data/ny_zhvi_df_2020_2024.csv")
zori_df = read.csv("data/ny_zori_df_2020_2024.csv")
nyc_boroughs = st_read("Borough Boundaries.geojson")
## Reading layer `Borough Boundaries' from data source 
##   `/Users/williamshie/Documents/Data Science/Final/final_2/Borough Boundaries.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 5 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25559 ymin: 40.49613 xmax: -73.70001 ymax: 40.91553
## Geodetic CRS:  WGS 84
head(rat_df)
##   inspection_type job_ticket_or_work_order_id    job_id job_progress        bbl
## 1         Initial                    13659068 PC8078519            1 4023290019
## 2         Initial                    13361613 PC7824966            1 3072810182
## 3         Initial                    13599220 PC8056345            1 2033950047
## 4            BAIT                     2941952 PC8141304            5 4104480015
## 5         Initial                    13788881 PC8254435            1 2057930467
## 6      Compliance                    13383745 PC7780758            2 3034140068
##   boro_code block lot house_number      street_name zip_code x_coord y_coord
## 1         4  2329  19        48-03        59 STREET    11377       0       0
## 2         3  7281 182          145 SEABREEZE AVENUE    11224       0       0
## 3         2  3395  47          530  EAST 234 STREET    10470       0       0
## 4         4 10448  15       192-21   JAMAICA AVENUE    11423 1048743  199167
## 5         2  5793 467         3555    OXFORD AVENUE    10463 1009450  262094
## 6         3  3414  68            9    COVERT STREET    11207       0       0
##   latitude longitude  borough        inspection_date             result
## 1 40.73837 -73.90647   Queens 06/05/2023 03:19:22 PM             Passed
## 2 40.57565 -73.97020 Brooklyn 04/05/2022 03:50:52 PM             Passed
## 3 40.89732 -73.86322    Bronx 03/14/2023 08:40:50 AM Failed for Other R
## 4 40.71310 -73.76736   Queens 12/26/2023 12:16:51 PM       Bait applied
## 5 40.88602 -73.90886    Bronx 01/23/2024 04:10:00 PM       Rat Activity
## 6 40.68518 -73.91342 Brooklyn 05/09/2022 03:35:15 PM             Passed
##            approved_date                            location community_board
## 1 06/08/2023 05:06:47 PM                                                   2
## 2 04/08/2022 03:54:46 PM                                                  13
## 3 03/17/2023 02:48:23 PM                                                  12
## 4 12/27/2023 10:10:53 AM (40.713101329334, -73.767362471917)              12
## 5 01/24/2024 11:33:03 AM (40.886019879068, -73.908861936068)               8
## 6 05/12/2022 08:19:56 AM                                                   4
##   council_district census_tract     bin                      nta year month
## 1               26          245 4053773                 Woodside 2023     6
## 2               48        35602 3196597    Coney Island-Sea Gate 2022     4
## 3               11        45101 2019758       Wakefield-Woodlawn 2023     3
## 4               23          482 4222146                   Hollis 2023    12
## 5               11          295 2084152 Riverdale-Spuyten Duyvil 2024     1
## 6               37          401 3078992          Bushwick (East) 2022     5
head(food_scrap_df)
##         borough                nta_name
## 1      Brooklyn               Bay Ridge
## 2     Manhattan East Midtown-Turtle Bay
## 3     Manhattan          Hell's Kitchen
## 4     Manhattan East Midtown-Turtle Bay
## 5     Manhattan    Tribeca-Civic Center
## 6 Staten Island St. George-New Brighton
##                                    site_name
## 1             4th Avenue Presbyterian Church
## 2         Dag Hammarskjold Plaza Greenmarket
## 3 Hudson River Park's Pier 84 at W. 44th St.
## 4                   58th Street Library FSDO
## 5                        Tribeca Greenmarket
## 6                     St. George Greenmarket
##                              site_addr
## 1  6753 4th Avenue, Brooklyn, NY 11220
## 2                  E 47th St & 2nd Ave
## 3 Pier 84 at W. 44th St. near dog park
## 4                 127 East 58th Street
## 5             Greenwich St. & Duane St
## 6            St. Marks Pl and Hyatt St
##                                                                      hosted_by
## 1                                               4th Avenue Presbyterian Church
## 2                                                                      GrowNYC
## 3                                                   Staff at Hudson River Park
## 4                                                                      GrowNYC
## 5                                                                      GrowNYC
## 6 NYC Compost Project Hosted by Snug Harbor Cultural Center & Botanical Garden
##   open_month                                             day_hours
## 1 Year Round        Every day (Start Time: Dawn - End Time:  Dusk)
## 2 Year Round Wednesday (Start Time: 8:00 AM - End Time:  12:30 PM)
## 3 Year Round  Every day (Start Time: 7:00 AM - End Time:  7:00 PM)
## 4 Year Round Wednesdays (Start Time: 7:30 AM - End Time:  1:30 PM)
## 5 Year Round   Saturday (Start Time: 8:00 AM - End Time:  1:00 PM)
## 6 Year Round   Saturday (Start Time: 8:00 AM - End Time:  1:00 PM)
##                       notes
## 1 No meat, bones, or dairy.
## 2                      <NA>
## 3                      <NA>
## 4                      <NA>
## 5                      <NA>
## 6                      <NA>
##                                                                          website
## 1                                                                           <NA>
## 2                                                            grownyc.org/compost
## 3 https://hudsonriverpark.org/the-park/sustainability/community-compost-program/
## 4                                                            grownyc.org/compost
## 5                                                            grownyc.org/compost
## 6                                                                snug-harbor.org
##   boro_cd council_dis  ct2010 bbl bin latitude longitude police_prec object_id
## 1     310          47 3012600  NA  NA 40.63551 -74.02277          68     31263
## 2     106           4 1009000  NA  NA 40.75261 -73.96904          17     31123
## 3     104           3 1012901  NA  NA 40.76346 -74.00025          18     31091
## 4     105           4 1011203  NA  NA 40.76198 -73.96930          18     31084
## 5     101           1 1003900  NA  NA 40.71742 -74.01079           1     30996
## 6     501          49 5000300  NA  NA 40.64197 -74.07793         120     30956
##                 location_point app_android app_i_os assembly_district
## 1 POINT (-74.022767 40.635514)        <NA>     <NA>                51
## 2 POINT (-73.969036 40.752606)        <NA>     <NA>                74
## 3   POINT (-74.00025 40.76346)        <NA>     <NA>                67
## 4    POINT (-73.9693 40.76198)        <NA>     <NA>                73
## 5 POINT (-74.010793 40.717424)        <NA>     <NA>                66
## 6 POINT (-74.077928 40.641967)        <NA>     <NA>                61
##   congress_district dsny_district dsny_section dsny_zone senate_district
## 1                10         BKS10       BKS101       BKS              17
## 2                12          MN06        MN063        MN              28
## 3                12          MN04        MN043        MN              47
## 4                12          MN05        MN052        MN              28
## 5                10          MN01        MN013        MN              27
## 6                11          SI01        SI012        SI              23
head(zhvi_df)
##   RegionID SizeRank RegionName RegionType StateName State     City
## 1    62080        4      11368        zip        NY    NY New York
## 2    62093        7      11385        zip        NY    NY New York
## 3    62019        9      11208        zip        NY    NY New York
## 4    62046       16      11236        zip        NY    NY New York
## 5    61807       17      10467        zip        NY    NY New York
## 6    62085       18      11373        zip        NY    NY New York
##                                   Metro    CountyName X2020.1.31 X2020.2.29
## 1 New York-Newark-Jersey City, NY-NJ-PA Queens County   528192.6   527257.9
## 2 New York-Newark-Jersey City, NY-NJ-PA Queens County   766285.8   766877.9
## 3 New York-Newark-Jersey City, NY-NJ-PA  Kings County   584069.0   584128.8
## 4 New York-Newark-Jersey City, NY-NJ-PA  Kings County   556447.0   558779.1
## 5 New York-Newark-Jersey City, NY-NJ-PA  Bronx County   431082.0   426699.4
## 6 New York-Newark-Jersey City, NY-NJ-PA Queens County   675125.7   675670.1
##   X2020.3.31 X2020.4.30 X2020.5.31 X2020.6.30 X2020.7.31 X2020.8.31 X2020.9.30
## 1   524266.9   522275.8   521385.9   520385.5   516688.1   510385.4   504947.1
## 2   767261.6   768287.1   768536.4   767880.3   766388.6   764911.0   763249.7
## 3   585511.1   588232.9   591933.8   592971.8   591628.3   589038.7   587388.2
## 4   561201.6   563977.4   566000.7   565868.6   564319.2   562457.2   561660.1
## 5   417166.3   409128.2   404095.7   402165.0   402087.6   401990.7   405016.7
## 6   673500.0   668760.7   664276.7   662083.9   654935.9   644841.3   635070.8
##   X2020.10.31 X2020.11.30 X2020.12.31 X2021.1.31 X2021.2.28 X2021.3.31
## 1    503106.5    503648.4    506265.0   508977.3   512132.8   514154.6
## 2    759656.1    756066.8    753333.5   752381.3   753657.6   756193.4
## 3    586779.6    587397.1    588714.3   590735.8   593778.1   597376.2
## 4    561378.6    562520.3    564734.6   568278.6   572763.3   578465.5
## 5    408095.2    412207.6    416081.1   421889.1   431480.8   444899.4
## 6    630178.7    626333.1    623006.2   618409.3   617337.5   618486.7
##   X2021.4.30 X2021.5.31 X2021.6.30 X2021.7.31 X2021.8.31 X2021.9.30 X2021.10.31
## 1   513545.6   513274.3   512169.7   513647.7   515205.9   517516.9    520167.9
## 2   760293.4   764712.1   768415.4   771259.6   772388.5   773570.8    775890.5
## 3   602246.5   606895.0   609155.7   610491.7   610604.1   613045.0    617421.0
## 4   584991.4   591055.9   594904.9   596912.1   597603.3   597795.9    598878.2
## 5   455437.9   462904.9   461875.1   453605.1   438793.4   421945.6    411599.9
## 6   619560.2   620293.7   617666.3   618197.6   618781.9   619884.8    620588.8
##   X2021.11.30 X2021.12.31 X2022.1.31 X2022.2.28 X2022.3.31 X2022.4.30
## 1    521725.6    521886.5   519710.8   517534.2   516469.2   516859.7
## 2    779431.8    782268.2   783578.9   783441.1   785378.3   788904.9
## 3    623356.3    628199.3   630765.7   631970.0   633731.6   636190.4
## 4    601476.8    604396.6   607793.6   611521.1   616361.0   620093.7
## 5    403827.1    397758.7   394423.8   398191.9   407522.6   413924.8
## 6    619487.9    617252.4   614656.6   614966.0   617660.6   619955.0
##   X2022.5.31 X2022.6.30 X2022.7.31 X2022.8.31 X2022.9.30 X2022.10.31
## 1   516227.9   516353.1   516371.5   514825.7   511602.0    508097.2
## 2   796084.2   804432.2   810063.0   811312.9   807884.0    804876.2
## 3   640465.9   645628.6   649119.5   649810.3   647188.8    644657.8
## 4   622656.0   625384.0   627041.1   627201.2   623448.9    620023.4
## 5   414126.5   412279.0   408672.8   402399.6   392358.9    384807.9
## 6   619968.6   621096.5   619121.3   612323.0   601194.8    591469.8
##   X2022.11.30 X2022.12.31 X2023.1.31 X2023.2.28 X2023.3.31 X2023.4.30
## 1    506580.5    503289.1   497724.8   490815.4   484453.6   477787.1
## 2    802838.2    802151.0   800703.4   798950.1   797761.4   799366.6
## 3    642264.1    641399.5   640105.2   637786.4   636126.6   640210.0
## 4    616886.2    614528.1   610691.6   606678.5   604931.1   608805.4
## 5    380556.4    379171.4   374879.9   373540.1   375699.4   384275.2
## 6    584564.3    577927.4   569925.2   564723.3   561582.7   556310.6
##   X2023.5.31 X2023.6.30 X2023.7.31 X2023.8.31 X2023.9.30 X2023.10.31
## 1   470639.5   465210.9   463377.3   463371.3   462810.6    460771.9
## 2   801223.4   802036.8   801422.6   800283.2   798291.5    797014.0
## 3   648346.8   654695.9   657573.2   657020.5   656315.8    655178.3
## 4   615239.9   621151.8   624614.7   626492.5   627861.0    628682.6
## 5   391838.2   396577.9   399856.9   402236.0   403255.3    400097.0
## 6   545838.3   531434.2   521217.5   513782.0   509985.5    505959.8
##   X2023.11.30 X2023.12.31 X2024.1.31 X2024.2.29 X2024.3.31 X2024.4.30
## 1    457397.8    453086.2   450240.7   448627.2   451705.6   456243.1
## 2    795779.1    795640.7   794282.4   794342.2   797380.4   802193.9
## 3    654648.3    656263.8   658709.2   661959.5   666440.0   670735.0
## 4    629011.8    630674.1   632713.9   635540.9   638623.8   641376.0
## 5    394496.2    386985.9   382000.0   378034.4   377532.6   374809.3
## 6    501444.1    497843.2   493967.4   492163.3   491429.0   492810.0
##   X2024.5.31 X2024.6.30 X2024.7.31 X2024.8.31 X2024.9.30 X2024.10.31
## 1   460893.5   461600.4   461088.3   460451.8   461417.2    461167.7
## 2   805219.7   804711.7   804494.0   806317.1   810315.1    814334.5
## 3   673816.0   674124.7   674583.6   675525.6   677539.8    678312.6
## 4   642619.7   641839.5   641629.1   643184.2   646821.1    650637.9
## 5   372827.7   369063.0   368696.4   369597.6   371425.2    371800.4
## 6   495404.8   497218.9   499719.7   501979.1   503272.5    505398.0
head(zori_df)
##   RegionID SizeRank RegionName RegionType StateName State     City
## 1    62080        4      11368        zip        NY    NY New York
## 2    62093        7      11385        zip        NY    NY New York
## 3    62019        9      11208        zip        NY    NY New York
## 4    62046       16      11236        zip        NY    NY New York
## 5    61807       17      10467        zip        NY    NY New York
## 6    62085       18      11373        zip        NY    NY New York
##                                   Metro    CountyName X2020.1.31 X2020.2.29
## 1 New York-Newark-Jersey City, NY-NJ-PA Queens County         NA         NA
## 2 New York-Newark-Jersey City, NY-NJ-PA Queens County   2448.553   2456.115
## 3 New York-Newark-Jersey City, NY-NJ-PA  Kings County         NA         NA
## 4 New York-Newark-Jersey City, NY-NJ-PA  Kings County         NA         NA
## 5 New York-Newark-Jersey City, NY-NJ-PA  Bronx County   1701.668   1720.197
## 6 New York-Newark-Jersey City, NY-NJ-PA Queens County         NA         NA
##   X2020.3.31 X2020.4.30 X2020.5.31 X2020.6.30 X2020.7.31 X2020.8.31 X2020.9.30
## 1         NA         NA         NA         NA         NA         NA         NA
## 2   2441.699   2414.372   2414.585   2408.849   2416.578   2369.325   2343.459
## 3         NA         NA         NA         NA         NA         NA         NA
## 4         NA         NA         NA         NA         NA         NA         NA
## 5   1722.498   1702.781   1658.133   1644.647   1707.506   1753.169   1799.189
## 6         NA         NA         NA         NA         NA         NA         NA
##   X2020.10.31 X2020.11.30 X2020.12.31 X2021.1.31 X2021.2.28 X2021.3.31
## 1          NA          NA          NA         NA         NA         NA
## 2    2311.269    2277.187    2248.553   2233.961   2224.142   2225.220
## 3          NA          NA          NA         NA         NA         NA
## 4          NA          NA          NA         NA         NA         NA
## 5    1775.834    1826.802    1776.000   1796.651   1753.189   1789.808
## 6          NA          NA          NA         NA         NA         NA
##   X2021.4.30 X2021.5.31 X2021.6.30 X2021.7.31 X2021.8.31 X2021.9.30 X2021.10.31
## 1         NA         NA         NA         NA         NA         NA          NA
## 2   2223.716   2263.851   2310.595   2361.465   2391.628   2421.895    2458.039
## 3         NA         NA         NA         NA         NA         NA          NA
## 4         NA         NA         NA         NA         NA         NA          NA
## 5   1741.302   1766.672   1775.301   1807.147   1802.181   1833.622    1871.474
## 6   1894.899   1906.100   1990.802   2012.177   2031.377   2045.573    1997.025
##   X2021.11.30 X2021.12.31 X2022.1.31 X2022.2.28 X2022.3.31 X2022.4.30
## 1          NA          NA         NA         NA         NA         NA
## 2    2493.205    2522.206   2537.980   2538.440   2549.701   2592.160
## 3          NA          NA         NA         NA   2285.018   2303.287
## 4          NA          NA         NA         NA         NA         NA
## 5    1870.410    1799.790   1772.979   1830.934   1881.293   1931.986
## 6    2020.485    2008.840   2007.273   2050.381   2088.892   2164.162
##   X2022.5.31 X2022.6.30 X2022.7.31 X2022.8.31 X2022.9.30 X2022.10.31
## 1         NA         NA         NA         NA         NA          NA
## 2   2665.379   2764.065   2823.873   2873.828   2878.283    2887.580
## 3   2362.293   2379.109   2352.997   2367.711   2348.447    2408.106
## 4   2226.912   2282.301   2265.703   2310.780   2305.516    2432.515
## 5   1924.659   1948.381   1985.830   1992.896   1981.437    1944.423
## 6   2170.632   2232.924   2264.414   2251.311   2230.028    2227.687
##   X2022.11.30 X2022.12.31 X2023.1.31 X2023.2.28 X2023.3.31 X2023.4.30
## 1          NA          NA         NA         NA         NA         NA
## 2    2860.363    2825.818   2787.999   2785.287   2797.609   2833.309
## 3    2436.682    2481.746   2502.737   2501.262   2493.180   2514.730
## 4    2415.473    2499.901   2345.235   2336.725   2292.325   2355.621
## 5    1944.814    1951.174   1987.002   2002.966   2018.190   1964.398
## 6    2250.507    2255.664   2288.122   2298.252   2300.585   2296.878
##   X2023.5.31 X2023.6.30 X2023.7.31 X2023.8.31 X2023.9.30 X2023.10.31
## 1         NA         NA         NA         NA         NA          NA
## 2   2875.260   2914.789   2929.464   2938.923   2952.872    2954.371
## 3   2533.124   2596.065   2575.544   2558.233   2550.770    2590.156
## 4   2415.095   2506.227   2545.207   2531.421   2575.470    2566.624
## 5   2017.347   2094.580   2121.534   2136.091   2214.768    2275.591
## 6   2295.696   2331.748   2350.530   2391.866   2376.860    2397.750
##   X2023.11.30 X2023.12.31 X2024.1.31 X2024.2.29 X2024.3.31 X2024.4.30
## 1          NA          NA         NA         NA         NA         NA
## 2    2938.652    2905.363   2900.733   2897.087   2948.450   2958.387
## 3    2695.514    2745.312   2781.866   2760.926   2739.610   2679.047
## 4    2655.016    2670.999   2766.037   2809.330   2878.693   2825.073
## 5    2348.462    2280.554   2321.299   2290.718   2304.462   2342.780
## 6    2384.238    2373.139   2372.172   2362.518   2372.199   2392.090
##   X2024.5.31 X2024.6.30 X2024.7.31 X2024.8.31 X2024.9.30 X2024.10.31
## 1         NA         NA   2495.950   2451.786   2696.151    2627.583
## 2   3024.869   3054.979   3123.694   3121.569   3085.313    3037.952
## 3   2606.380   2638.616   2594.905   2669.834   2700.378    2966.500
## 4   2853.657   2756.713   2775.097   2882.028   2992.892    2998.333
## 5   2317.591   2345.020   2332.908   2306.385   2200.925    2252.289
## 6   2470.749   2536.356   2588.668   2568.784   2596.982    2514.722
# Filter for rows where the result indicates rodent activity
data_rodent <- rat_df %>% filter(grepl("Rat Activity", result, ignore.case = TRUE))

# Group by neighborhood (NTA), year, and month to count occurrences of rodent activity
rodent_activity_summary <- data_rodent %>% 
  group_by(nta, year, month) %>% 
  summarise(count = n(), .groups = 'drop') %>% 
  arrange(desc(count))

# Print the top neighborhoods with the highest rodent activity
print(head(rodent_activity_summary))
## # A tibble: 6 × 4
##   nta                        year month count
##   <chr>                     <int> <int> <int>
## 1 Harlem (South)             2022     3    92
## 2 Crown Heights (North)      2022     3    89
## 3 Bedford-Stuyvesant (West)  2021    12    83
## 4 Harlem (South)             2022     8    80
## 5 Sunset Park (West)         2024     5    78
## 6 Flatbush                   2024     8    77
# Filter data for the year 2020
rodent_activity_2020 <- rodent_activity_summary %>% filter(year == 2020)

# Create a bar plot for rodent activity in 2020
ggplot(data = rodent_activity_2020, aes(x = month, y = count)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Rodent Activity in 2020",
       x = "month",
       y = "Rodent Activity Count") 

Purpose:

The bar plot shows how rodent activity varies across months in 2020. This helps identify seasonal trends, such as higher rodent activity in summer months. Findings:

There is a clear peak in rodent activity during the summer months (July and August). A significant drop in activity is observed in the spring (April). Justification:

Understanding monthly trends in rodent activity can inform targeted pest control strategies during peak seasons.

# Aggregate data by year and month
yearly_monthly_summary <- data_rodent %>%
  group_by(year, month) %>%
  summarise(total_activity = n(), .groups = 'drop') %>%
  arrange(year, month)

# Create a line plot showing rodent activity over time
ggplot(yearly_monthly_summary, aes(x = as.numeric(month), y = total_activity, color = as.factor(year), group = year)) +
  geom_line(size = 1) +
  geom_point() +
  scale_x_continuous(breaks = 1:12) +
  labs(title = "Monthly Rodent Activity by Year (2020-2024)",
       x = "Month",
       y = "Total Rodent Activity Reports",
       color = "Year") +
  theme_minimal() +
  theme(legend.position = "right")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Objective: To identify patterns in rodent activity across months and years. To explore temporal trends and assess year-over-year consistency.

Findings: Rodent activity generally peaks during the summer months (June to September), regardless of the year. In some years, there are anomalies (e.g., dips in October for 2024). A gradual increase in activity is evident over the years, suggesting either more rodents or improved reporting mechanisms.

# Analyze the distribution of food scrap drop-off sites by borough
borough_distribution <- food_scrap_df %>% 
  group_by(borough) %>% 
  summarise(count = n()) %>% 
  arrange(desc(count))

# Display the borough distribution
print("Borough Distribution:")
## [1] "Borough Distribution:"
print(borough_distribution)
## # A tibble: 5 × 2
##   borough       count
##   <chr>         <int>
## 1 Manhattan       251
## 2 Brooklyn        171
## 3 Bronx            75
## 4 Queens           75
## 5 Staten Island    19
# Borough distribution plot
ggplot(borough_distribution, aes(x = reorder(borough, -count), y = count)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  theme_minimal() +
  labs(title = "Distribution of Food Scrap Drop-off Sites by Borough",
       x = "Borough",
       y = "Number of Sites") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Objective: Analyze the geographic distribution of food scrap drop-off sites across NYC boroughs. Identify whether disparities exist in access to food scrap sites between boroughs.

Findings: Manhattan has the highest number of drop-off sites, followed by Brooklyn. Bronx, Queens, and Staten Island have significantly fewer sites. The disparity suggests possible inequities in access to food scrap recycling programs, particularly for Staten Island residents.

# Analyze the temporal patterns
# First, let's create a simplified season categorization
food_scrap_df <- food_scrap_df %>%
  mutate(
    operation_type = case_when(
      grepl("Year Round", open_month, ignore.case = TRUE) ~ "Year Round",
      grepl("Seasonal|Spring|Summer|Fall|Winter", open_month, ignore.case = TRUE) ~ "Seasonal",
      TRUE ~ "Specific Months"
    )
  )

# Create a summary of operational patterns
operation_summary <- food_scrap_df %>%
  group_by(operation_type) %>%
  summarise(count = n())

print("Operation Pattern Summary:")
## [1] "Operation Pattern Summary:"
print(operation_summary)
## # A tibble: 3 × 2
##   operation_type  count
##   <chr>           <int>
## 1 Seasonal            5
## 2 Specific Months    27
## 3 Year Round        559
# Create a scatter plot of locations
ggplot(food_scrap_df, aes(x = longitude, y = latitude)) +
  geom_point(aes(color = borough), alpha = 0.6) +
  theme_minimal() +
  labs(title = "Geographic Distribution of Food Scrap Drop-off Sites",
       x = "Longitude",
       y = "Latitude") +
  theme(legend.position = "right")

Objective: To analyze the spatial distribution of food scrap drop-off sites across boroughs. To visualize clustering and disparities in drop-off site availability.

Findings: Drop-off sites are densely clustered in Manhattan and Brooklyn. Sparse distribution in Staten Island reflects fewer food scrap programs in this borough. Queens and Bronx show moderate distribution but lack the density seen in Manhattan.

plot_county <- ggplot(zhvi_df, aes(x = CountyName, fill = CountyName)) +
  geom_bar() +
  labs(
    title = "Demographic Differences by Borough",
    x = "Borough",
    y = "Count",
    fill = "Borough"
  ) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 8, angle = 90, hjust = 1, vjust = 0.5),  # Rotate and resize x-axis text
    plot.title = element_text(size = 16, face = "bold"),  # Enlarge title
    axis.title.x = element_text(size = 14),  # Enlarge x-axis label
    axis.title.y = element_text(size = 14),  # Enlarge y-axis label
    legend.position = "none"  # Remove legend to reduce clutter
  )

print(plot_county)

Objective: Analyze the geographic distribution of food scrap drop-off sites across NYC boroughs. Identify whether disparities exist in access to food scrap sites between boroughs. Findings: Manhattan has the highest number of drop-off sites, followed by Brooklyn. Bronx, Queens, and Staten Island have significantly fewer sites. The disparity suggests possible inequities in access to food scrap recycling programs, particularly for Staten Island residents.

# Filter rodent activity data for July
rodent_july <- data_rodent %>% filter(month == 8)

# Assuming food_scrap_df is available, filter it for July operations
food_scrap_july <- food_scrap_df %>% filter(grepl("July", open_month, ignore.case = TRUE))

# Create a scatter plot showing both rodent activity and food scrap sites in July
ggplot() +
  geom_point(data = rodent_july, aes(x = longitude, y = latitude, color = "Rodent Activity"), alpha = 0.6) +
  geom_point(data = food_scrap_july, aes(x = longitude, y = latitude, color = "Food Scrap Sites"), alpha = 0.6) +
  theme_minimal() +
  labs(title = "Interaction of Rodent Activity and Food Scrap Sites in July",
       x = "Longitude",
       y = "Latitude",
       color = "Legend") +
  theme(legend.position = "right")

# Load necessary libraries
library(leaflet)

# Filter rodent activity data for July
rodent_july <- data_rodent %>% filter(month == 8)

# Define NYC boroughs data (assuming nyc_boroughs is preloaded)
# Colors for boroughs
colors <- setNames(c("#FF0005", "#F0FDF0", "#5030FF", "#FFEF00", "#DF00F0"), unique(nyc_boroughs$boro_name))

# Create the leaflet map
rat_map <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap, options = providerTileOptions(noWrap = TRUE)) %>%
  setView(lng = -74.0060, lat = 40.7128, zoom = 11) %>%
  addPolygons(data = nyc_boroughs,
              fillColor = ~colors[as.character(nyc_boroughs$boro_name)],
              fillOpacity = 0.5,
              color = colors,
              weight = 1,
              popup = ~nyc_boroughs$boro_name) %>%
  addCircleMarkers(data = rodent_july,
                   lng = ~longitude, lat = ~latitude,
                   radius = 3,
                   color = "#00F0FF",
                   fillOpacity = 0.5,
                   popup = paste("<strong>Borough: </strong>", rodent_july$borough,
                                 "<br><strong>Date: </strong>", format(as.Date(paste0(rodent_july$year, "-", rodent_july$month, "-01")), "%Y-%m")))

# Print the map
rat_map
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.
## Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON.

Objective: The map overlays rodent activity data and food scrap drop-off sites for July, a peak month for rodent activity. The visualization aims to understand the spatial relationship between these two factors.

Clustered Rodent Activity:

Rodent activity is heavily concentrated in high-density urban areas, particularly Manhattan and parts of Brooklyn. Sparse activity is observed in Staten Island, which aligns with its lower population density. Food Scrap Drop-off Sites:

The distribution of food scrap sites shows a notable overlap with high rodent activity areas, particularly in Manhattan and Brooklyn. Staten Island has both fewer rodent activity reports and fewer food scrap sites, indicating potential inequity in access to waste management resources.